TARGET=test
CC=arm-none-eabi-gcc
CP=arm-none-eabi-objcopy

FLASH_SIZE=-D STM32F10X_HD 	#可以在stm32f10x.h开头中添加 #defind STM32F10X.HD

LDFLAGS = -T stm32_flash.ld -Wl,-cref,-u,Reset_Handler -Wl,-Map=$(TARGET).map -Wl,--gc-sections -Wl,--defsym=malloc_getpagesize_P=0x80 -Wl,--start-group -lc -lm -Wl,--end-group
CFLAGS=-mthumb -mcpu=cortex-m3 -g -o
HARD = $(patsubst %.c,%.o,$(wildcard ./*.c))

$(TARGET).hex:$(TARGET).elf
	$(CP) $< -Oihex $@

$(TARGET).elf:startup_stm32f10x_hd.o $(HARD)
	$(CC) $^ $(LDFLAGS) $(CFLAGS) $@

startup_stm32f10x_hd.o:startup_stm32f10x_hd.s
	$(CC) -c $< $(CFLAGS) $@

./%.o:./%.c ./%.h
	$(CC) -c $< $(CFLAGS) $@ $(FLASH_SIZE) 

.PHONY: bin hex flash clean
bin:
	$(CP) $(TARGET).elf $(TARGET).bin

hex:
	$(CP) $(TARGET).elf -Oihex $(TARGET).hex

flash:
	st-flash --format ihex write $(TARGET).hex

clean:
	rm -f *.o $(TARGET).*
